package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirAirportMapper;
import zhongbo.java.air.dictionary.model.AirAirport;
import zhongbo.java.air.dictionary.model.AirAlertZone;
import zhongbo.java.air.dictionary.service.AirAirportService;

import java.util.List;
import java.util.Optional;

/**
 * 机场信息表(AirAirport)表服务实现类
 *
 * @author makejava
 * @since 2025-03-31 14:05:43
 */
@Service("airAirportService")
public class AirAirportServiceImpl implements AirAirportService<AirAirport> {
    @Autowired
    private AirAirportMapper airAirportMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param entityId 主键
     * @return 实例对象
     */
//    @Override
//    public AirAirport queryById(String id) throws Exception {
//        if (id == null){
//            throw new Exception("id不能为空");
//        }
//        Optional<AirAirport> airAirportOptional = Optional.ofNullable(airAirportMapper.selectById(id));
//        if (!airAirportOptional.isPresent()){
//            throw new Exception("id不存在");
//        }
//        return airAirportOptional.get();
//    }
    @Override
    public AirAirport findOne(String entityId) throws Exception {
        if (entityId == null){
            throw new Exception("id不能为空");
        }
        Optional<AirAirport> airAirportOptional = Optional.ofNullable(airAirportMapper.selectById(entityId));
        if (!airAirportOptional.isPresent()){
            throw new Exception("id不存在");
        }
        return airAirportOptional.get();
    }


    @Override
    public PageVo<AirAirport> findPage(int page, int pageSize, String keyword, String fields, String oderByField, boolean orderByAsc) {
        QueryWrapper<AirAirport> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
        IPage<AirAirport> pageParam = new Page<>(page, pageSize);
        IPage airAlertZoneIPage = airAirportMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(airAlertZoneIPage);
    }


    /**
     * 新增数据
     *
     * @param entity 实例对象
     * @return 实例对象
     */
//    @Override
//    public AirAirport insert(AirAirport airAirport) throws Exception {
//        if (airAirport == null){
//            throw new Exception("新增失败");
//        }
//
//        // 字段重复验证
//        validateUniqueFields(airAirport);
//
//        this.airAirportMapper.insert(airAirport);
//        return airAirport;
//    }
    @Override
    public AirAirport add(AirAirport entity) throws Exception {
        if (entity == null){
            throw new Exception("新增失败");
        }

        // 字段重复验证
        validateUniqueFields(entity);

        this.airAirportMapper.insert(entity);
        return entity;
    }

    /**
     * 修改数据
     *
     * @param edit 实例对象
     * @return 实例对象
     */
//    @Override
//    public AirAirport update(AirAirport airAirport) throws Exception {
//        if (airAirport == null){
//            throw new Exception("更新失败");
//        }
//
//        // 字段重复验证
//        validateUniqueFields(airAirport);
//
//        this.airAirportMapper.updateById(airAirport);
//        return this.queryById(airAirport.getId());
//    }
    @Override
    public AirAirport edit(AirAirport edit) throws Exception {
        if (edit == null){
            throw new Exception("更新失败");
        }

        // 字段重复验证
        validateUniqueFields(edit);

        this.airAirportMapper.updateById(edit);
        return this.findOne(edit.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param entityId 主键
     * @return 是否成功
     */
//    @Override
//    public boolean deleteById(String id) throws Exception {
//        if (id == null){
//            throw new Exception("id不能为空");
//        }
//        return this.airAirportMapper.deleteById(id) > 0;
//    }
    @Override
    public boolean delete(String entityId) throws Exception {
        if (entityId == null){
            throw new Exception("id不能为空");
        }
        return this.airAirportMapper.deleteById(entityId) > 0;
    }

    // 添加字段重复验证方法
    private void validateUniqueFields(AirAirport airAirport) throws Exception {
        // 验证 icaoCode 和 iataCode 的唯一性
        Optional<AirAirport> existingByIcaoCode = Optional.ofNullable(airAirportMapper.selectByIcaoCode(airAirport.getIcaoCode()));
        if (existingByIcaoCode.isPresent()) {
            throw new Exception("ICAO 机场代码已存在");
        }
        Optional<AirAirport> existingByIataCode = Optional.ofNullable(airAirportMapper.selectByIataCode(airAirport.getIataCode()));
        if (existingByIataCode.isPresent()) {
            throw new Exception("IATA 代码已存在");
        }
    }
}